import store from '../store/index.js'
// 封装请求对象
const BASR_URL = 'https://api.imooc-blog.lgdsunday.club/api'
function request({ url, data, method }) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: BASR_URL + url,
      data: data,
      method: method,
      header: {
        Authorization: store.getters.token
      },
      success: ({ data }) => {
        if (data.success) {
          // 请求成功
          resolve(data.data)
        } else {
          // 请求失败
          // 给用户一个提示
          uni.showToast({
            title: data.message,
            icon: 'none',
            duration: 3000
          })
          reject(data.message)
        }
      },
      fail: err => {
        reject(err)
      },
      // 请求成功和失败都会执行
      complete: () => {
        uni.hideLoading()
      }
    })
  })
}
export default request
